/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-07 20:09:53
 */
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import helmet from 'helmet';
import { ValidationPipe } from './common/admin.pipe';
import { TransformInterceptor } from './common/transform.interceptor';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { HttpExceptionFilter } from './common/http-exception.filter';
import * as express from 'express';
import { DefaultDataService } from './DefaultDataService.service';
import { ConfigService } from '@nestjs/config';
import { ConfigurationType } from '../config/configuration';
import { Logger } from '@nestjs/common';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(express.json());
  app.use(express.urlencoded({ extended: true }));
  //   全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 参数校验
  app.useGlobalPipes(new ValidationPipe());
  // 拦截器转换格式
  app.useGlobalInterceptors(new TransformInterceptor());
  // 门路前缀：如：http://www.test.com/api/v1/user
  app.setGlobalPrefix('');
  //cors：跨域资源共享，形式一：容许跨站拜访
  app.enableCors();
  //避免跨站脚本攻打
  app.use(helmet());
  const config = new DocumentBuilder()
    .addBearerAuth()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('后台管理用户', 'admin-user')
    .addTag('分类', 'category')
    .addTag('聊天记录', 'chat')
    .addTag('权限', 'role')
    .addTag('用户', 'user')
    .addTag('图片上传', 'upload')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('swagger', app, document);
  /** 第一次运行执行初始化角色用户 */
  const defaultDataService = app.get(DefaultDataService);
  await defaultDataService.checkAndAddDefaultData();
  const logger: Logger = new Logger('main.ts');
  /** 获取默认配置 */
  const configService = app.get(ConfigService<ConfigurationType>);
  const PORT = configService.get('port');
  const HOST = configService.get('host');
  const PREFIX = `/${configService.get('prefix')}`;
  const PROJECTNAME = configService.get('projectName');
  app.setGlobalPrefix(PREFIX);
  await app.listen(PORT, HOST, () => {
    logger.log(
      `[${PROJECTNAME}]已经启动,接口请访问: http://${HOST}:${PORT}${PREFIX}swagger`,
    );
  });
  console.log('https://localhost:3003/swagger');
}
bootstrap();
